{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vertical Magnetic Dipole over a Sphere (frequency domain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline\n",
    "from IPython.display import display\n",
    "from geoscilabs.em.HarmonicVMDCylWidget import HarmonicVMDCylWidget\n",
    "vmd = HarmonicVMDCylWidget()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import rcParams\n",
    "rcParams['font.size'] = 16"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "Here, we show fields and fluxes that result from placing a transient vertical magnetic dipole (VMD) source over an Earth model containing a spherical structure and an overburden layer. \n",
    "\n",
    "There are [two commonly used models](https://em.geosci.xyz/content/maxwell1_fundamentals/dipole_sources_in_homogeneous_media/magnetic_dipole_definition/index.html) for describing a harmonic VMD source: 1) as an infinitessimally small bar magnet that experiences a harmonic magnetization in the vertical direction, and 2) as an infinitessimally small horizontal loop of wire carrying a harmonic current. In either case, the harmonic VMD creates a primary harmonic magnetic field in its viscinity; which is vertical at the location of the source.\n",
    "\n",
    "True dipole sources do not exist in nature however they can be approximated in practice. For geophysical applications, small wire loops carrying harmonic current are used to approximate harmonic VMD sources. These EM sources may be placed on the Earth's surface (ground-based surveys) or flown through the air (airborne surveys). Because the source's primary field is harmonic, it induces anomalous currents in the Earth. The distribution and strength of the induced currents depends on the frequency of the harmonic VMD source and the subsurface conductivity distribution. The induced current ultimately produce secondary magnetic fields which can be measured by one or more receivers.\n",
    "\n",
    "In this app, we explore the following:\n",
    "\n",
    "- How does the frequency impact the fields and currents produced by the harmonic VMD source?\n",
    "- For our geological model, how does changing the dimensions and conductivity of the layer and sphere impact the fields and currents produced by the harmonic VMD source?\n",
    "- How does the frequency impact secondary fields measured above the Earth's surface?\n",
    "- For our geological model, how does changing the dimensions and conductivity of the layer and sphere impact secondary fields measured above the Earth's surface?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup\n",
    "\n",
    "The geological scenario being modeled is shown in the figure below. Here, the Earth is comprised of a spherical structure and overburden layer within a homogeneous background. Each of the three geological units has its own conductivity ($\\sigma$). However, a constant magnetic susceptibility ($\\chi$) is used for all units; where $\\mu_0$ is the magnetic permeability of free space and $\\mu = \\mu_0 (1 +\\chi)$. The depth to the layer is given by $d_1$ and $h$.\n",
    "\n",
    "In this case, a harmonic VMD source (*Tx*) is used to excite the Earth, and the Earth's FEM response (secondary magnetic field) is measured by a receiver (*Rx*). In practice, the transmitter and receiver may be placed near the Earth's surface or in the air. The source may also operate at a variety of frequencies. Because we are operating in the frequency domain, fields and currents within the region have both real and imaginary components.\n",
    "\n",
    "To understand the fields and currents resulting from a harmonic VMD source over a layered Earth we have two apps:\n",
    "\n",
    "- **Fields app:** Models the fields and currents everywhere at a particular frequency\n",
    "- **Data app:** Models the secondary magnetic field observed at the receiver as a function of frequency\n",
    "\n",
    "\n",
    "<img src=\"https://github.com/geoscixyz/geosci-labs/blob/master/images/em/LayerSphereFEM.png?raw=true\"></img>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercise\n",
    "\n",
    "**Follow the exercise in a linear fashion. Some questions may use parameters set in a previous question.**\n",
    "\n",
    "\n",
    "- **Q1:** Set $\\sigma_b$, $\\sigma_1$ and $\\sigma_2$ to arbitrary conductivity values. Based on the geometry of the problem, which components (x, y, z) of each field (E, B, Bsec or J) are zero? Run the *Fields app* and set *AmpDir = None*. Next, try different combinations of *Field* and *Comp*. Does the simulation match what you initially thought?\n",
    "\n",
    "\n",
    "- **Q2:** Re-run the *Fields app* to set parameters back to default. Note that $\\sigma_b = \\sigma_1 = 0.0001$ S/m and $\\sigma_2 = 1$ S/m. Now set *f index = 1* (resistive limit), *Field = J* and *Re/Im = Amp*. Begin to increase *f index* from 1 to 31. Answer the following:\n",
    "    1. From *f index = 16* to *f index = 24* the currents start to be only visible on the edges of the conductor. Why is this? *Hint: consider skin depth inside the conductor*.\n",
    "    2. As *f index* $\\rightarrow$ *31*, where is the largest concentration of currents? Why?\n",
    "\n",
    "\n",
    "- **Q3:** Now set *f index = 21*. What happens to the distribution of current density if the layer conductivity is increased from $\\sigma_1$ = 0.0001 S/m to 0.001, then to 0.01 S/m, then to 0.1 S/m? Explain.\n",
    "\n",
    "\n",
    "- **Q4:** Re-run the *Fields app* to set parameters back to default. Set *Field = B*, *AmpDir = Direction*, *Re/Im = Re* and *f index = 1* for a vector plot of the primary magnetic field; since at low enough frequencies, the secondary field is negligible compared to the primary field. Now switch *Field = Bsec* and use *Re/Im* to answer the following:\n",
    "    1. Compare the real and imaginary component of Bsec. Which is bigger in maximum amplitude?\n",
    "    2. Near (x,z)=(0,0), is the largest component (Re or Im) in the same direction as the primary field or opposite?\n",
    "    3. Repeat 1 and 2 using *f index = 31*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fields app\n",
    "\n",
    "We use this app to simulate the fields and currents everywhere due to a harmonic VMD source. The fields and induced currents depend on the frequency of the harmonic VMD source and the subsurface conductivity distribution. You will use the app to change various parameters in the model and see how the fields and currents change.\n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": total magnetic flux density, \"Bsec\": secondary magnetic flux density, \"J\": current density and \"Model\": conductivity model)\n",
    "- **AmpDir:** If *None* is selected, then the *x*, *y* or *z* component chosen on the next line is plotted. If *Direction* is chosen, a vector plot is plotted (only possible for B and Bsec)\n",
    "- **Comp.:** If *None* is selected on the previous line, the user chooses whether the *x*, *y* or *z* component is plotted.     \n",
    "- **Re/Im:** Type of complex data (\"Re\", \"Im\", \"Amp\", \"Phase\") \n",
    "- $\\boldsymbol{\\sigma_0}$: Conductivity above z=0m (leave as 1e-8) in S/m\n",
    "- $\\boldsymbol{\\sigma_b}$: Background conductivity below z = 0 m in S/m\n",
    "- $\\boldsymbol{\\sigma_1}$: Conductivity of the layer in S/m\n",
    "- $\\boldsymbol{\\sigma_2}$: Conductivity of the sphere in S/m\n",
    "- $\\boldsymbol{\\chi}$: Susceptibility below z = 0 m in SI\n",
    "- $\\boldsymbol{d_1}$: Depth to the top of the layer\n",
    "- $\\boldsymbol{h_1}$: Thickness of the layer\n",
    "- $\\boldsymbol{d_2}$: Depth to the centre of the sphere\n",
    "- $\\boldsymbol{R}$: Radius of the sphere\n",
    "- **Scale:** Plot data values on *log-scale* or *linear-scale*\n",
    "- $\\boldsymbol{\\Delta x}$ (m): Horizontal separation distance between the transmitter and receiver\n",
    "- $\\boldsymbol{\\Delta z}$ (m): Height of the transmitter and receiver above the Earth's surface\n",
    "- **f index:** Frequency index for the set of frequencies models by this app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6cc24eafedd8433a993ed1ff3a633121",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(ToggleButtons(description='Field', options=('E', 'B', 'Bsec', 'J', 'Model'), value='E'), Toggl…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q1 = vmd.InteractivePlane_Sphere()\n",
    "display(Q1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data app\n",
    "\n",
    "Using this app, we show how the fields observed at the receiver location depend on the parameters set in the previous app. *Note that if you want to see changes in the data due to changes in the model, you MUST* re-run the previous app. \n",
    "\n",
    "## Parameters:\n",
    "\n",
    "- **Field:** Type of EM fields (\"E\": electric field, \"B\": total magnetic flux density, \"Bsec\": secondary magnetic flux density)\n",
    "- **Comp.:** Direction of EM field at Rx locations        \n",
    "- **Scale:** Scale of y-axis values (\"log\" or \"linear\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9aa19d0a094f43de95d362364988eaff",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MyApp(children=(ToggleButtons(description='Field', index=1, options=('E', 'B', 'Bsec'), value='B'), ToggleButt…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Q2 = vmd.InteractiveData_Sphere()\n",
    "display(Q2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Explore\n",
    "\n",
    "EM fields will be depenent upon a number of parameters, using a simple half-space model ($\\sigma_1=\\sigma_2=\\sigma_3$) explore how EM fields and data changes upon below four parameters. \n",
    "\n",
    "- E1: Effects of frequency?\n",
    "\n",
    "\n",
    "- E2: Effects of Tx height?\n",
    "\n",
    "\n",
    "- E3: Effects of Conductivity?\n",
    "\n",
    "\n",
    "- E4: Effects of Susceptibility?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "widgets": {
   "state": {
    "c0dd4dbce2ff4d0cacf7363e6fdfed13": {
     "views": [
      {
       "cell_index": 6
      }
     ]
    },
    "d6ee822b25404d33979ba6ec5f19963c": {
     "views": [
      {
       "cell_index": 8
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
